define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'event/pep/index',
                    add_url: 'event/pep/add',
                    edit_url: 'event/pep/edit',
                    del_url: 'event/pep/del',
                    multi_url: 'event/pep/multi',
                    byOut_url:'event/pep/by_out',
                    table: 'pep',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'url', title: __('Url'), formatter: Controller.api.formatter.avatar},
                        // {field: 'openid', title: __('Openid')},
                        {field: 'name', title: __('Name')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'shen_text', title: __('Shen'),operate: false},
                        {field: 'shi_text', title: __('Shi'),operate: false},
                        {field: 'xian_text', title: __('Xian'),operate: false},
                        {field: 'address', title: __('Address')},
                        {field: 'version', title: __('Version')},
                        {field: 'pay', title: __('Pay'), searchList: {"0":__('Pay 0'),"1":__('Pay 1'),"2":__('Pay 2')}, formatter: Table.api.formatter.normal},
                        {field: 'out_trade_no', title: __('Out_trade_no')},
                        {field: 'sum', title: __('Sum'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Controller.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
          formatter: {//渲染的方法
            avatar: function (value, row, index) {
              return '<img width="45" height="45" src="'+value+'" class="img-circle">';
            },
            operate: function (value, row, index) {
              var table = this.table;
              // 操作配置
              var options = table ? table.bootstrapTable('getOptions') : {};
              // 默认按钮组
              var buttons = $.extend([], this.buttons || []);
              if (options.extend.byOut_url !== ''){
                buttons.push({
                  name: 'books',
                  icon: 'fa fa-paper-plane',
                  title: __('退款'),
                  text:'退款',
                  extend: 'data-toggle="tooltip"',
                  area:['50%', '80%'],
                  classname: 'btn btn-xs btn-success btn-out',
                  url: options.extend.byOut_url,
                  hidden:function (row) {
                    if(row.pay == 2 || row.pay == 0)
                    {
                      return true;
                    }
                    return false;
                  }
                });
              }
              if (options.extend.edit_url !== ''){
                buttons.push({
                  name: 'edit',
                  icon: 'fa fa-pencil',
                  title: __('Edit'),
                  extend: 'data-toggle="tooltip"',
                  classname: 'btn btn-xs btn-success btn-editone',
                  url: options.extend.edit_url
                });
              }
              if (options.extend.del_url !== '') {
                buttons.push({
                  name: 'del',
                  icon: 'fa fa-trash',
                  title: __('Del'),
                  extend: 'data-toggle="tooltip"',
                  classname: 'btn btn-xs btn-danger btn-delone',
                  url: options.extend.del_url
                });
              }
              return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
            },
          }
        }
    };
    return Controller;
});